package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.astrob.activitys.GPSProvider;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.database.manager.QiyaCenter;
import com.nhc.model.SensorSetInfo;

import java.text.DecimalFormat;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.util.StringUtil;

public class QiyaSetActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "QiyaSetActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, QiyaSetActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qiyaset_activity);

        _setInfo = SensorSetInfo.getInstance();
        _setInfo.load(this);
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private RadioGroup rg_qiya;
    private RadioButton radioButton_0;
    private RadioButton radioButton_1;
    private RadioButton radioButton_2;

    TextView tv_gps;
    EditText edit_input_1;
    EditText edit_input_2;

    TextView tv_unit_1;
    TextView tv_unit_2;

    double h0 = 0;//当前GPS高度
    double qiya = 0;//当前气压值 帕
    double qiyaH = 0;//当前GPS高度

    @Override
    public void initView() {//必须调用

        rg_qiya = (RadioGroup) findViewById(R.id.rg_qiya);
        radioButton_0 = (RadioButton) findViewById(R.id.radioButton_0);
        radioButton_1 = (RadioButton) findViewById(R.id.radioButton_1);
        radioButton_2 = (RadioButton) findViewById(R.id.radioButton_2);
        radioButton_0.setChecked(true);
        radioButton_1.setChecked(false);
        radioButton_2.setChecked(false);
        rg_qiya.setOnCheckedChangeListener(new MyRadioButtonListener());

        tv_gps = (TextView) findViewById(R.id.tv_gps);
        edit_input_1 = (EditText) findViewById(R.id.edit_input_1);
        edit_input_2 = (EditText) findViewById(R.id.edit_input_2);

        tv_unit_1 = (TextView) findViewById(R.id.tv_unit_1);
        tv_unit_2 = (TextView) findViewById(R.id.tv_unit_2);

        h0 = GPSProvider.getInstance().getAltitude();
        if (QiyaCenter.getInstance()._thisQiyaInfo != null) {
            qiyaH = QiyaCenter.getInstance()._thisQiyaInfo.getAltitude();
        }


        String ALT[] = SysUtils.getInstance().getAltitudeStr((int) (qiyaH + 0.5));
        edit_input_1.setHint(getString(R.string.qygd) + " " + ALT[0] + ALT[1]);

        qiya = SensorSetInfo.getInstance().getHeight_qiya();
        String QIY[] = SysUtils.getInstance().getQiyaStr(qiya * 100);
        edit_input_2.setHint(getString(R.string.qyz) + QIY[0] + QIY[1]);

        _select = SensorSetInfo.getInstance().getQiyamode();
        switch (_select) {
            case 0:
                radioButton_0.setChecked(true);
                radioButton_1.setChecked(false);
                radioButton_2.setChecked(false);
                break;
            case 1:
                radioButton_1.setChecked(true);
                radioButton_0.setChecked(false);
                radioButton_2.setChecked(false);
                break;
            case 2:
                radioButton_2.setChecked(true);
                radioButton_1.setChecked(false);
                radioButton_0.setChecked(false);
                break;
        }

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    SensorSetInfo _setInfo;

    @Override
    public void initData() {//必须调用
        String DIS[] = SysUtils.getInstance().getDistanceStr((int) GPSProvider.getInstance().getAltitude());
        tv_gps.setText(getString(R.string.gpsgd) + DIS[0] + DIS[1]);

        String strUnit_0 = "m";
        switch (SysUtils.getInstance().dis_mode) {
            default:
                break;
            case 1:
            case 4:
                //ft => m
                strUnit_0 = "ft";
                break;
            case 2: {
                //yd => m
                strUnit_0 = "yd";
            }
            break;
        }
        tv_unit_1.setText(strUnit_0);

        String strUnit_1 = "m";
        switch (SysUtils.getInstance().qiya_mode) {
            default:
            case 0:
                strUnit_1 = "mba";
                break;
            case 1:
                strUnit_1 = "pa";
                break;
            case 2:
                strUnit_1 = " in Hg";
                break;
            case 3:
                strUnit_1 = " mm Hg";
                break;
        }
        tv_unit_2.setText(strUnit_1);

    }

    int _select = 0;

    class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.radioButton_0:
                    _select = 0;
                    break;
                case R.id.radioButton_1:
                    _select = 1;
                    break;
                case R.id.radioButton_2:
                    _select = 2;
                    break;
            }
        }
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        edit_input_1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String inputedString = StringUtil.getTrimedString(s);
                String temp = StringUtil.getNumberD(inputedString);

                if (temp.compareToIgnoreCase(inputedString) != 0) {
                    edit_input_1.setText(temp);
                    int index = temp.length();
                    edit_input_1.setSelection(index);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable edt) {

            }
        });

        edit_input_2.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String inputedString = StringUtil.getTrimedString(s);
                String temp = StringUtil.getNumberD(inputedString);

                if (temp.compareToIgnoreCase(inputedString) != 0) {
                    edit_input_2.setText(temp);
                    int index = temp.length();
                    edit_input_2.setSelection(index);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable edt) {

            }
        });

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        SensorSetInfo.getInstance().setQiyamode(_select);
        _setInfo.save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onForwardClick(View v) {
        String strH1 = StringUtil.getNumberD(edit_input_1.getText().toString());
        String strH2 = StringUtil.getNumberD(edit_input_2.getText().toString());

        double h = h0;
        float height_qiya = 0;
        switch (_select) {
            default:
            case 0:
                break;
            case 1:
                try {
                    h = Double.valueOf(strH1);
                } catch (NumberFormatException e) {
                    showShortToast(getString(R.string.qsrdqzqgd));
                    return;
                }
                break;
            case 2:
                try {
                    height_qiya = Float.valueOf(strH2) + 0.0005f;
                } catch (NumberFormatException e) {
                    showShortToast(getString(R.string.qsrzdyhpmqy));
                    return;
                }

                switch (SysUtils.getInstance().qiya_mode) {
                    default:
                    case 0:
                        break;
                    case 1:
                        height_qiya /= 100;
                        break;
                    case 2:
                        height_qiya *= 33.863882;
                        break;
                    case 3:
                        height_qiya *= 1.3332237;
                        break;
                }

                break;
        }

        if (height_qiya != 0) {
            if (Math.abs(height_qiya - 1013.2) > 1000) {
                showShortToast(getString(R.string.zdyhpmqypcgd));
                return;
            }
            SensorSetInfo.getInstance().setHeight_qiya(height_qiya);
        } else if (QiyaCenter.getInstance()._thisQiyaInfo != null) {
            double sPV = QiyaCenter.getInstance()._thisQiyaInfo.getSPV();
            SensorSetInfo.getInstance().useHeightCalibration(h, (float) sPV);
        }

        if (_select > 0) {
            SensorSetInfo.getInstance().setGpsautojz(false);
        } else {
            SensorSetInfo.getInstance().setGpsautojz(true);
        }
        SensorSetInfo.getInstance().save(this);

        setResult(RESULT_OK);
        finish();
    }

    private double sqrt(double d, double i) {
        i = 1 / i;
        return Math.pow(d, i);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
